class Sample:
    def __enter__(self):
        #获取资源
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        #释放资源
        print("exit")

    def hello(self):
        print("hello")


# with Sample() as s:
#     s.hello()

#Sample().hello()


import contextlib

@contextlib.contextmanager
def safe_file_writer(file_name):
    f = open(file_name,mode="w",encoding="utf-8")
    print("打开文件句柄")
    try:
        yield f
    finally:
        f.close()
        print("关闭文件句柄")


with safe_file_writer('a.log') as f:
    f.write("阿道夫")


from collections import abc